﻿using jiaju.Areas.Back.Models;
using jiaju.Areas.Back.Models.DAL;
using jiaju.Areas.Back.Models.DBModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class ProductController : Controller
    {
        // GET: Back/Product
        public ActionResult Index()
        {         
            return View();
        }

        public ActionResult getProduct(int page, int limit, string name,string isPub,string type,string texture)
        {
            List<Product> dataList = ProductService.getProductList(page, limit, name, isPub);
            string sql = "select COUNT(*) from dbo.product ";
            int cout = (int)DBHelp.ExcuteScalar(sql);
            Object obj = new
            {
                code = 0,
                msg = "请求成功",
                count = cout,
                data = dataList
            };
            return Json(obj, JsonRequestBehavior.AllowGet);
        }    
        public ActionResult Add()
        {
            List<ProductType> list = ProductTypeService.getAllName();
            ViewBag.A = list;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddDel(Product art)
        {
            int num = ProductService.AddProduct(art);
            if(num>0)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }
        }
        public ActionResult Edit(string Id)
        {
            Product art = ProductService.GetById(Id);
            ViewBag.A = art;
            List<ProductType> list = ProductTypeService.getAllName();
            ViewBag.B = list;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditDel(Product art)
        {
            int m = ProductService.EditProduct(art);
            if (m > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败",m });
            }
        }
        public ActionResult DelArt(String Id)
        {
            int n = ProductService.DelProduct(Id);
            if (n > 0)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }
        }
      
        [HttpPost]
        public ActionResult UploadPhoto()
        {
            HttpPostedFileBase postFile = Request.Files["uplaodImg"];
            if (postFile != null)
            {
                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";              
                string fullUpLoadPath = Server.MapPath("~/images/");
                postFile.SaveAs(fullUpLoadPath + newFileName);
                return Json(new { success = true, path = "images/" + newFileName, msg = "上传成功" });
            }
            else
            {
                return Json(new { success = false, path = "", msg = "上传失败" });
            }
        }
        [HttpPost]
        public ActionResult UploadLayedit()
        {
            HttpPostedFileBase postFile = Request.Files["file"];
            if (postFile != null)
            {

                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                string uploadPath = "~/images/";
                string fullUploadPath = Server.MapPath(uploadPath);

                postFile.SaveAs(fullUploadPath + newFileName);
                return Json(new { code = 0, msg = "上传成功", data = new { src = "/images/" + newFileName } });
            }
            else
            {
                return Json(new { code = -1, msg = "失败", data = new { src = "" } });
            }

        }

    }
}